import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:oktoast/oktoast.dart';
import 'package:yurun_smartmerchants/receiver/event_bus.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/utils/dialog_utils.dart';
import 'package:yurun_smartmerchants/page/mine.dart';
import 'workspace.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => new _HomePageState();
}

final List<BottomNavigationBarItem> bottomBar = <BottomNavigationBarItem>[
  BottomNavigationBarItem(
      icon: Container(
        width: 40.w,
        height: 40.w,
        child: Image.asset(
          "images/home_normal.png",
          fit: BoxFit.cover,
//        width: 10,
//        height: 10,
        ),
      ),
      activeIcon: Container(
        width: 40.w,
        height: 40.w,
        child: Image.asset(
          "images/home_active.png",
          fit: BoxFit.cover,
//        width: 10,
//        height: 10,
        ),
      ),
      title: Text("工作台")),
  BottomNavigationBarItem(
      icon: Container(
        width: 40.w,
        height: 40.w,
        child: Image.asset(
          "images/mine_normal.png",
          fit: BoxFit.cover,
        ),
      ),
      activeIcon: Container(
        width: 40.w,
        height: 40.w,
        child: Image.asset(
          "images/mine_active.png",
          fit: BoxFit.cover,
        ),
      ),
      title: Text("我的")),
];

final List<Widget> pages = <Widget>[
  WorkSpace(),
  MinePage()
];


class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  DateTime lastPopTime;
  int currentIndex = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    super.build(context);
    return WillPopScope(
        child: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            selectedFontSize: 22.sp,
            unselectedFontSize: 22.sp,
            type: BottomNavigationBarType.fixed,
            currentIndex: this.currentIndex,
            onTap: (index) async {
              setState(() {
                this.currentIndex = index;
                pageController.jumpToPage(index);
              });
            },
            items: bottomBar,),
          body: _getPageBody(context),
        ),
        // ignore: missing_return
        onWillPop: () async {
          if(lastPopTime == null || DateTime.now().difference(lastPopTime) > Duration(seconds: 1)) {
            lastPopTime = DateTime.now();
            showToast('再按一次退出');
          }else {
            lastPopTime = DateTime.now();
            return await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
          }
        }
    );
  }

  final pageController = PageController();
  Widget _getPageBody(BuildContext context) {
    return PageView(
      controller: pageController,
      children: pages,
      physics: NeverScrollableScrollPhysics(),
    );
  }

  StreamSubscription _indexSubscription;

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
  void _listen() {
    _indexSubscription = eventBus.on<IndexInEvent>().listen((event) {
      int index = int.parse(event.index);
      this.currentIndex = index;
      pageController.jumpToPage(index);
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    pageController.dispose();
    _indexSubscription.cancel();
  }

}
